//
//  CZMainViewController.m
//  YingKeLive
//
//  Created by 西瓜大浪 on 2021/9/2.
//

#import "CZMainViewController.h"
#import "CZFollowController.h"
#import "CZHotController.h"
#import "CZNearbyController.h"

@interface CZMainViewController () <UIScrollViewDelegate>
@property(nonatomic,weak)UIScrollView *scrollView;



@end

@implementation CZMainViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //设置左右navigationItem
    [self setupleftButtonAndrightButton];
    
    //加载子视图
    [self setupAllChildvcs];
    
    //添加scrollView
    [self setUpScrollView];
    
    
}

//设置scrollView
-(void)setUpScrollView{
    
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    scrollView.backgroundColor = [UIColor whiteColor];
    scrollView.frame = [UIScreen mainScreen].bounds;
    scrollView.pagingEnabled = YES; //分页
    scrollView.showsVerticalScrollIndicator = NO; //垂直滚动条
//    scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView = scrollView;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    //设置ScrollView的大小
    scrollView.contentSize = CGSizeMake(SREENWIDTH * 3, 0);
    
    
    [self scrollViewDidEndDecelerating:self.scrollView];

}

//初始化子控制器
-(void)setupAllChildvcs{
    //关注
    CZFollowController *followVC = [[CZFollowController alloc]init];
    [self addChildViewController:followVC];
    
    //热门
    CZHotController *hotVC = [[CZHotController alloc]init];
    [self addChildViewController:hotVC];
    //附近
    CZNearbyController *nearbuVC = [[CZNearbyController alloc]init];
    [self addChildViewController:nearbuVC];
    
}


#pragma  mark -scrollview
//滚动结束
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    //获取偏移值
    CGFloat offsetx = self.scrollView.contentOffset.x;
    
    //通过偏移值求出当前显示哪一个页面的索引
    NSInteger idx = self.scrollView.contentOffset.x / SREENWIDTH;
        
    
    //通过索引加载对应的子视图
    UIViewController *ChiVC = self.childViewControllers[idx];

    if([ChiVC isViewLoaded])return; //如果视图已经加载过就return
    if(ChiVC.view.superview)return; //如果父类有当前view 就return 不用计算frame
    if(ChiVC.view.window)return; //如果wondow中有这个视图就return
    
    //没有加载过就设置X为offsetX
    ChiVC.view.frame = CGRectMake(offsetx, 0, SREENWIDTH, SREENHEIGHT);
    //将View添加到scrollview
    [self.scrollView addSubview:ChiVC.view];
    
}


#pragma  mark -Navigationitem
//设置左右navigationItem
-(void)setupleftButtonAndrightButton{
    
    //设置TopView;
    
    
    
    
    // 设置intrinsicContentSize
    


self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"global_search"] style:UIBarButtonItemStyleDone target:self action:@selector(searchClick:)];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"title_button_more"] style:UIBarButtonItemStyleDone target:self action:@selector(emailClick:)];
    
    
}

//搜索按钮
-(void)searchClick:(UIBarButtonItem *)sender{


        CZFunc;
}

-(void)emailClick:(UIBarButtonItem *)sender{
        CZFunc
    
}




@end
